package com.macro.mall.tiny.modules.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.macro.mall.tiny.modules.ums.mapper.UserCourseMapper;
import com.macro.mall.tiny.modules.ums.model.UserCourse;
import com.macro.mall.tiny.modules.ums.model.bo.CourseUserSelectBO;
import com.macro.mall.tiny.modules.ums.model.bo.UserCourseInstBO;
import com.macro.mall.tiny.modules.ums.service.UserCourseService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author: i_kun
 * @Desc: o_。
 * @create: 2025-06-26 14:47
 */
@Service
public class UserCourseServiceImpl extends ServiceImpl<UserCourseMapper, UserCourse> implements UserCourseService {

    /**
     * 用户与课程的分页查询
     *
     * @param courseUserSelectBO
     */
    @Override
    public Page selectPage(CourseUserSelectBO courseUserSelectBO) {
        QueryWrapper<UserCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotBlank(courseUserSelectBO.getCourserName())
                        ,"course_name", courseUserSelectBO.getCourserName());
        queryWrapper.eq("is_del", 1);
        queryWrapper.orderByDesc("create_time");
         Page  page = new Page<UserCourse>(courseUserSelectBO.getPageNum(),courseUserSelectBO.getPageSize());
         return baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 用户与课程的添加
     *
     * @param userCourseInstBO
     */
    @Override
    public void inst(UserCourseInstBO userCourseInstBO) {
        Set<Long> courseIdSet = userCourseInstBO.getCourseIdSet();
        Set<Long> userIdSet = userCourseInstBO.getUserIdSet();
        ArrayList<UserCourse> courseArrayList = new ArrayList<>();
        for (Long userId : userIdSet) {
            for (Long courseId : courseIdSet) {
                UserCourse userCourse = new UserCourse();
                userCourse.setUserId(userId);
                userCourse.setCourseId(courseId);
                userCourse.setIsDel(1);
                userCourse.setCreateBy(userCourseInstBO.getOperator());
                userCourse.setCreateTime(new Date());
                userCourse.setUpdateBy(userCourseInstBO.getOperator());
                userCourse.setUpdateTime(new Date());
                courseArrayList.add(userCourse);
            }
        }
       saveBatch(courseArrayList);
    }

    /**
     * 用户与课程的删除
     *
     * @param courseId 课程ID
     * @param userId   用户信息
     */
    @Override
    public void del(Long courseId, String userId,String username) {
        QueryWrapper<UserCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 1);
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("course_id", courseId);
        List<UserCourse> userCourses = baseMapper.selectList(queryWrapper);
        if (userCourses.size() > 0) {
            for (UserCourse userCourse : userCourses) {
                userCourse.setIsDel(0);
                userCourse.setUpdateBy(username);
                userCourse.setUpdateTime(new Date());
                baseMapper.updateById(userCourse);
            }
        }
    }
}
